package cn.sanshuilin.kst.system.provider.write.dao;

import cn.sanshuilin.kst.common.utils.Utility;
import cn.sanshuilin.kst.system.provider.bean.Dict;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;


/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2017-12-02 15:06
 * Copyright 2017  by 三水林科技有限公司
 **/
@Repository
public class DictReq extends AbstractJpaDao<Dict> {

    // 设置编码
    public Object setCode(Dict dict) {
        // 根据ParentID和CategoryID取得当前级别最大的编码
        String hql = " SELECT MAX(Code) FROM Dict  WHERE ParentID = :ParentID ";
        Query query = entityManager.createNativeQuery(hql);
        query.setParameter("ParentID", dict.getParentId());
        return  query.getSingleResult();
    }


    public Object getNewDict() {

        Query query = entityManager.createNativeQuery("SELECT MAX(ID) AS ID FROM Dict");
        Object strMaxID = query.getSingleResult();

        return strMaxID;


}


    public int getMaxSequence(int nParentID) {
        Query query = entityManager.createNativeQuery("SELECT MAX(SequenceNo) AS SequenceNo FROM Dict WHERE parentID = :ParentID");
        query.setParameter("ParentID", nParentID);
        Object strMaxID = query.getSingleResult();
        int nMaxID = Utility.toSafeInt(strMaxID, 1) + 2;

        return nMaxID;
    }
}
